package com.xcity.game.activity.recharge;

import com.xcity.db.entity.Player;
import com.xcity.game.activity.ActivityService;
import com.xcity.game.common.Cause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.event.GameEvents;
import com.xcity.game.item.ItemException;
import com.xcity.game.recharge.RechargeTemplate;
import com.xcity.pomelo.PomeloRoute;
import com.xcity.pomelo.request.FirstRechargeRewardRequest;
import com.xcity.pomelo.request.FirstRechargeStateRequest;
import com.xcity.pomelo.response.FirstRechargeStateResponse;
import com.xcity.pomelo.response.HintResponse;
import com.xcity.pomelo.response.Response;

import naga.x.App;
import naga.x.event.Event;
import naga.x.event.EventListener;
import naga.x.net.Session;
import naga.x.net.annotation.OPHandler;
import naga.x.net.impl.pomelo.annotation.PomeloOP;
import naga.x.service.ServiceException;

/**
 * 首冲活动
 * @author yang.li
 *
 */
@OPHandler
public class FirstRechargeService extends ActivityService implements EventListener {

	protected static final int RECHARGE_ID = 30003;
	protected static final int REWARD_ITEM_ID = 30097;
	
//	public static final byte STATE_UNRECHARGE = 0, STATE_RECHARGED = 1, STATE_REWARDED = 2;
	
	@Override
	public void startup() throws ServiceException {
		App.getApp().getEventManager().register(this);
	}

	@Override
	public void shutdown() throws ServiceException {
	}
	
	@PomeloOP(PomeloRoute.ACTIVITY_FIRST_RECHARGE_STATE)
	protected void requestInfo(Session session, FirstRechargeStateRequest req) {
		Player player = session.getClient();
		session.send(new FirstRechargeStateResponse(req.getSerial(), player.getPool().getByteValue(PlayerPool.PROPERTY_FIRST_RECHARGE_STATE)));
	}
	
	@PomeloOP(PomeloRoute.ACTIVITY_FIRST_RECHARGE_GET_REWARD)
	protected void requestReward(Session session, FirstRechargeRewardRequest req) {
		Player player = session.getClient();
		byte state = player.getPool().getByteValue(PlayerPool.PROPERTY_FIRST_RECHARGE_STATE);
		if (state == 1) {
			try {
				player.getBag().addItem(REWARD_ITEM_ID, 1, Cause.FIRST_RECHARGE);
				player.poolSet(PlayerPool.PROPERTY_FIRST_RECHARGE_STATE, (byte) 2);
				session.send(new Response(req.getSerial()));
			} catch (ItemException e) {
				session.send(new HintResponse(req.getSerial(), e.getMessage()));
			}
		} else if (state == 0) {
			session.send(new HintResponse(req.getSerial(), MessageConst.GET_REWARD_WHEN_UNRECHARGE));
		} else {
			session.send(new HintResponse(req.getSerial(), MessageConst.REWARD_ALREADY_RECEIVED));
		}
	}

	@Override
	public int[] getEventTypes() {
		return new int[] { GameEvents.EVENT_RECHARGE, GameEvents.EVENT_RECHARGE_OFFLINE };
	}

	@Override
	public void handleEvent(Event event) {
		Player player = event.getParameter(0);
//		int money = event.getParameter(1);
		RechargeTemplate template = event.getParameter(2);
		onRecharge(player, template);
	}

	private void onRecharge(Player player, RechargeTemplate template) {
//		if (money < 6) {
//			return;
//		}
		if (template.getId() == RECHARGE_ID) {
			byte state = player.getPool().getByteValue(PlayerPool.PROPERTY_FIRST_RECHARGE_STATE);
			if (state == 0) {
				player.poolSet(PlayerPool.PROPERTY_FIRST_RECHARGE_STATE, (byte) 1);
			}
		}
	}

}
